$(document).ready(function(){

	var agkuID = $("#agkuID").text();
	
	getCurrent(1,10,agkuID);
	 
	 $('#firstPage').click(function(){
	 	var page=1;
		var maxRow=$("#maxRow").text();
		getCurrent(page,maxRow,agkuID);
	 });
	 
	 $('#prePage').click(function(){
	 	var page=$("#page").text();
		page=(parseInt(page)-1);
		var maxRow=$("#maxRow").text();
		if(page<=0){
			alert("第一页了");
			return ;
		}
		getCurrent(page,maxRow,agkuID);
	 });
	 
	 $('#nextPage').click(function(){
	 	var page=$("#page").text();
		page=(parseInt(page)+1);
		var maxRow=$("#maxRow").text();
		var pageCount=$("#pageCount").text();
		if(page>=pageCount){
			alert("最后一页了");
			return;
		}
		getCurrent(page,maxRow,agkuID);
	 });
	 
	 $('#lastPage').click(function(){
	 	var page=$("#pageCount").text();
		var maxRow=$("#maxRow").text();
		getCurrent(page,maxRow,agkuID);
	 });
	 
	 $('#jumpPage').click(function(){
	 	var pageCount=$("#pageCount").text();
		var maxRow=$("#maxRow").text();
		var page=$("#jump").val();
		if(page>pageCount){
			alert("页数过大");
			return;
		}
		getCurrent(page,maxRow,agkuID);
		
	 });
	 
	 //新增物品
	 $("#add_goods").click(function(){
	 	var agkuID = $("#agkuID").text();
	 	window.location.href="/ag/hmk/GoodsAdd.jsp?vv="+agkuID;
	 });
	 
	 //返回库详情
	  $("#back_Agdetail").click(function(){
	 	var agkuID = $("#agkuID").text();
	 	window.location.href="/ag/hmk/findOneAGKU_findOneAgku.action?agkuID1="+agkuID; 
	 });
	 
});

	function getCurrent(page1,maxRow1,agkuID){
		var page =page1;
		var maxRow = maxRow1;
		$('#goodsBody').empty();	
		$("#loadingPerson").show();
		$.ajax({
  			type: "post",
 			url: "hmk/findOneAGKU_findGoods.action",
 			data:{
 				  agkuID1:agkuID,page:page,maxRow:maxRow
 				 },
  			success:showData
	 	});
	}

	function showData(data){
	   $('#goodsBody').empty();
		if(data.pageUtil.list.length==0){
			$("#loadingPerson").hide();
			$("#nonePerson").show();
			$("#total").text(0);
			$("#page").text(0);
			$("#pageCount").text(0);
			$("#maxRow").text(0);
		}else{
			$("#nonePerson").hide();
			$("#loadingPerson").hide();
			$.each(data.pageUtil.list,function(i,n){
				var gt = n.tbGoods.tbGoodstype;
				if(gt==null){
		  		  gt="";
				 }else{
		   		 gt=n.tbGoods.tbGoodstype.gtName;
				}
			fillDataToHtml(n.tbGoods.goodsId,n.tbGoods.goodsName,n.agNumber,n.tbGoodsstatus.gstatusName,
					 n.tbGoods.tbUnit.unitName, n.tbGoods.goodsDetail,gt);
	 		});
	  }
	
	  $('#total').text(data.pageUtil.total);
	  $('#page').text(data.pageUtil.page);
	  $('#pageCount').text(Math.ceil(data.pageUtil.total/10));
	  $('#maxRow').text(data.pageUtil.maxRow);
	}

	function fillDataToHtml(goodid,goodName,goodNum,goodStatus,gooodUnit,goodDetail,goodType){
		var strHtml="<tr>";
		strHtml+="<td height='30px' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
        strHtml+=goodName+"</div></td>";
        strHtml+="<td height='30px' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
        strHtml+=goodNum+"</div></td>";
        strHtml+="<td height='30px' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
        strHtml+=gooodUnit+"</div></td>";
        strHtml+="<td height='30px' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
        strHtml+=goodStatus+"</div></td>";
        strHtml+="<td height='30px' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
        strHtml+=goodDetail+"</div></td>";
        strHtml+="<td height='30px' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
        strHtml+=goodType+"</div></td>";
        
        strHtml+="<td height='30px' bgcolor='#FFFFFF'><div align='center'><img src='hmk/images/037.gif' width='9' height='9' /><span class='STYLE1'> [</span><a style='cursor:pointer' onclick='editGoods(this)'>编辑</a>";
        	strHtml+="<span  style='display:none'>";
	    	strHtml+=goodid;
	    	strHtml+="</span>";
        strHtml+="<span class='STYLE1'>]</span></div></td>";
        strHtml+="<td height='30px' bgcolor='#FFFFFF'><div align='center'><span class='STYLE2'><img src='hmk/images/010.gif' width='9' height='9' /> </span><span class='STYLE1'>[</span><a style='cursor:pointer' onclick='delGoods(this)'>删除</a>";
        	strHtml+="<span  style='display:none'>";
	    	strHtml+=goodid;
	    	strHtml+="</span>";
        strHtml+="<span class='STYLE1'>]</span></div></td>"
        strHtml+="</tr>";
        
        $('#goodsBody').append(strHtml);
	}

	//编辑物资
	function editGoods(v){
		var goodsID = $(v).next().text();
		var agkuID  =$("#agkuID").text();
		window.location.href="/ag/hmk/findOneGoods_findOneGoods.action?agkuID1="+agkuID+"&goodsID1="+goodsID;
	}

	//删除物资
	function delGoods(v){
		var isPass = confirm("确定要删除吗？");
		if(isPass){
			var gID = $(v).next().text();
			var agkuID  =$("#agkuID").text();
			var url = 'hmk/saveAidGoodsKU_deleteGood.action';
			var params = {
				agkuID1:agkuID,gID1:gID
			}; 
			jQuery.post(url, params, delData, 'json');
		}
		
	}

	function delData(data){
		if(data.ajaxRST=='1'){
			alert("删除成功");
			var agkuID  =$("#agkuID").text();
			var timestamp = Date.parse(new Date());
			window.location.href="/ag/hmk/findOneAGKU_findAGKUByID.action?agkuID1="+agkuID+"&ts="+timestamp;
		 }else{
			alert("删除失败");
	 	}
	}